package services

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/url"
)

func push_feishu_text(key string, pushType GitPushType, content string) int {

	return 0
}

func push_feishu_markdown(key string, pushType GitPushType, content string) int {
	uri, _ := url.JoinPath("https://open.feishu.cn/open-apis/bot/v2/hook", key)
	md_map := map[string]any{
		"msg_type": "interactive",
		"card": map[string]any{
			"header": map[string]any{
				"title": map[string]any{
					"tag":     "plain_text",
					"content": PushTitle[pushType],
				},
				"template": "blue",
			},
			"elements": []map[string]any{
				{
					"tag":     "markdown",
					"content": content,
				},
				{
					"tag": "hr",
				},
				{
					"tag":        "markdown",
					"text_align": "right",
					"content":    "🎉🎉🎉",
				},
			},
		},
	}
	data, _ := json.Marshal(md_map)
	reader := bytes.NewReader(data)
	resp, err := http.Post(uri, "application/json", reader)
	if err != nil {
		return 0
	}
	defer resp.Body.Close()
	data, err = io.ReadAll(resp.Body)
	if err != nil {
		return 0
	}
	fmt.Println("push_feishu_markdown Post Feishu done", string(data))
	return 1
}
